<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>submenu.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_submenu.js.html">submenu.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A class representing menu items that open a submenu.
<a name="line17"></a> * @see goog.ui.Menu
<a name="line18"></a> *
<a name="line19"></a> * @see ../demos/submenus.html
<a name="line20"></a> * @see ../demos/submenus2.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.SubMenu&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line29"></a>goog.require(&#39;goog.positioning.AnchoredViewportPosition&#39;);
<a name="line30"></a>goog.require(&#39;goog.positioning.Corner&#39;);
<a name="line31"></a>goog.require(&#39;goog.style&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line34"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line35"></a>goog.require(&#39;goog.ui.ControlContent&#39;);
<a name="line36"></a>goog.require(&#39;goog.ui.Menu&#39;);
<a name="line37"></a>goog.require(&#39;goog.ui.MenuItem&#39;);
<a name="line38"></a>goog.require(&#39;goog.ui.SubMenuRenderer&#39;);
<a name="line39"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Class representing a submenu that can be added as an item to other menus.
<a name="line45"></a> *
<a name="line46"></a> * @param {goog.ui.ControlContent} content Text caption or DOM structure to
<a name="line47"></a> *     display as the content of the submenu (use to add icons or styling to
<a name="line48"></a> *     menus).
<a name="line49"></a> * @param {*=} opt_model Data/model associated with the menu item.
<a name="line50"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional dom helper used for dom
<a name="line51"></a> *     interactions.
<a name="line52"></a> * @param {goog.ui.MenuItemRenderer=} opt_renderer Renderer used to render or
<a name="line53"></a> *     decorate the component; defaults to {@link goog.ui.SubMenuRenderer}.
<a name="line54"></a> * @constructor
<a name="line55"></a> * @extends {goog.ui.MenuItem}
<a name="line56"></a> */
<a name="line57"></a>goog.ui.SubMenu = function(content, opt_model, opt_domHelper, opt_renderer) {
<a name="line58"></a>  goog.ui.MenuItem.call(this, content, opt_model, opt_domHelper,
<a name="line59"></a>                        opt_renderer || goog.ui.SubMenuRenderer.getInstance());
<a name="line60"></a>};
<a name="line61"></a>goog.inherits(goog.ui.SubMenu, goog.ui.MenuItem);
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * The delay before opening the sub menu in milliseconds.
<a name="line66"></a> * @type {number}
<a name="line67"></a> */
<a name="line68"></a>goog.ui.SubMenu.MENU_DELAY_MS = 218;
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * Timer used to dismiss the submenu when the item becomes unhighlighted.
<a name="line73"></a> * @type {?number}
<a name="line74"></a> * @private
<a name="line75"></a> */
<a name="line76"></a>goog.ui.SubMenu.prototype.dismissTimer_ = null;
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Timer used to show the submenu on mouseover.
<a name="line81"></a> * @type {?number}
<a name="line82"></a> * @private
<a name="line83"></a> */
<a name="line84"></a>goog.ui.SubMenu.prototype.showTimer_ = null;
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Flag used to determine if the submenu has control of the keyevents.
<a name="line89"></a> * @type {boolean}
<a name="line90"></a> * @private
<a name="line91"></a> */
<a name="line92"></a>goog.ui.SubMenu.prototype.hasKeyboardControl_ = false;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * The lazily created sub menu.
<a name="line97"></a> * @type {goog.ui.Menu?}
<a name="line98"></a> * @private
<a name="line99"></a> */
<a name="line100"></a>goog.ui.SubMenu.prototype.subMenu_ = null;
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Whether or not the sub-menu was set explicitly.
<a name="line105"></a> * @type {boolean}
<a name="line106"></a> * @private
<a name="line107"></a> */
<a name="line108"></a>goog.ui.SubMenu.prototype.externalSubMenu_ = false;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Whether or not to align the submenu at the end of the parent menu.
<a name="line113"></a> * If true, the menu expands to the right in LTR languages and to the left
<a name="line114"></a> * in RTL langauges.
<a name="line115"></a> * @type {boolean}
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.ui.SubMenu.prototype.alignToEnd_ = true;
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Whether the position of this submenu may be adjusted to fit
<a name="line123"></a> * the visible area, as in {@link goog.ui.Popup.positionAtCoordinate}.
<a name="line124"></a> * @type {boolean}
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.ui.SubMenu.prototype.isPositionAdjustable_ = false;
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/** @override */
<a name="line131"></a>goog.ui.SubMenu.prototype.enterDocument = function() {
<a name="line132"></a>  goog.ui.SubMenu.superClass_.enterDocument.call(this);
<a name="line133"></a>
<a name="line134"></a>  this.getHandler().listen(this.getParent(), goog.ui.Component.EventType.HIDE,
<a name="line135"></a>      this.onParentHidden_);
<a name="line136"></a>
<a name="line137"></a>  if (this.subMenu_) {
<a name="line138"></a>    this.setMenuListenersEnabled_(this.subMenu_, true);
<a name="line139"></a>  }
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/** @override */
<a name="line144"></a>goog.ui.SubMenu.prototype.exitDocument = function() {
<a name="line145"></a>  this.getHandler().unlisten(this.getParent(), goog.ui.Component.EventType.HIDE,
<a name="line146"></a>      this.onParentHidden_);
<a name="line147"></a>
<a name="line148"></a>  if (this.subMenu_) {
<a name="line149"></a>    this.setMenuListenersEnabled_(this.subMenu_, false);
<a name="line150"></a>    if (!this.externalSubMenu_) {
<a name="line151"></a>      this.subMenu_.exitDocument();
<a name="line152"></a>      goog.dom.removeNode(this.subMenu_.getElement());
<a name="line153"></a>    }
<a name="line154"></a>  }
<a name="line155"></a>
<a name="line156"></a>  goog.ui.SubMenu.superClass_.exitDocument.call(this);
<a name="line157"></a>};
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/** @override */
<a name="line161"></a>goog.ui.SubMenu.prototype.disposeInternal = function() {
<a name="line162"></a>  if (this.subMenu_ &amp;&amp; !this.externalSubMenu_) {
<a name="line163"></a>    this.subMenu_.dispose();
<a name="line164"></a>  }
<a name="line165"></a>  this.subMenu_ = null;
<a name="line166"></a>  goog.ui.SubMenu.superClass_.disposeInternal.call(this);
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * @override
<a name="line172"></a> * Dismisses the submenu on a delay, with the result that the user needs less
<a name="line173"></a> * accuracy when moving to submenus.  Alternate implementations could use
<a name="line174"></a> * geometry instead of a timer.
<a name="line175"></a> * @param {boolean} highlight Whether item should be highlighted.
<a name="line176"></a> * @param {boolean=} opt_btnPressed Whether the mouse button is held down.
<a name="line177"></a> */
<a name="line178"></a>goog.ui.SubMenu.prototype.setHighlighted = function(highlight,
<a name="line179"></a>                                                    opt_btnPressed) {
<a name="line180"></a>  goog.ui.SubMenu.superClass_.setHighlighted.call(this, highlight);
<a name="line181"></a>
<a name="line182"></a>  if (opt_btnPressed) {
<a name="line183"></a>    this.getMenu().setMouseButtonPressed(true);
<a name="line184"></a>  }
<a name="line185"></a>
<a name="line186"></a>  if (!highlight) {
<a name="line187"></a>    if (this.dismissTimer_) {
<a name="line188"></a>      goog.Timer.clear(this.dismissTimer_);
<a name="line189"></a>    }
<a name="line190"></a>    this.dismissTimer_ = goog.Timer.callOnce(
<a name="line191"></a>        this.dismissSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
<a name="line192"></a>  }
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Show the submenu and ensure that all siblings are hidden.
<a name="line198"></a> */
<a name="line199"></a>goog.ui.SubMenu.prototype.showSubMenu = function() {
<a name="line200"></a>  // Only show the menu if this item is still selected. This is called on a
<a name="line201"></a>  // timeout, so make sure our parent still exists.
<a name="line202"></a>  var parent = this.getParent();
<a name="line203"></a>  if (parent &amp;&amp; parent.getHighlighted() == this) {
<a name="line204"></a>    this.setSubMenuVisible_(true);
<a name="line205"></a>    this.dismissSiblings_();
<a name="line206"></a>    this.keyboardSetFocus_ = false;
<a name="line207"></a>  }
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/**
<a name="line212"></a> * Dismisses the menu and all further submenus.
<a name="line213"></a> */
<a name="line214"></a>goog.ui.SubMenu.prototype.dismissSubMenu = function() {
<a name="line215"></a>  // Because setHighlighted calls this function on a timeout, we need to make
<a name="line216"></a>  // sure that the sub menu hasn&#39;t been disposed when we come back.
<a name="line217"></a>  var subMenu = this.subMenu_;
<a name="line218"></a>  if (subMenu &amp;&amp; subMenu.getParent() == this) {
<a name="line219"></a>    this.setSubMenuVisible_(false);
<a name="line220"></a>    subMenu.forEachChild(function(child) {
<a name="line221"></a>      if (typeof child.dismissSubMenu == &#39;function&#39;) {
<a name="line222"></a>        child.dismissSubMenu();
<a name="line223"></a>      }
<a name="line224"></a>    });
<a name="line225"></a>  }
<a name="line226"></a>};
<a name="line227"></a>
<a name="line228"></a>
<a name="line229"></a>/**
<a name="line230"></a> * Clears the show and hide timers for the sub menu.
<a name="line231"></a> */
<a name="line232"></a>goog.ui.SubMenu.prototype.clearTimers = function() {
<a name="line233"></a>  if (this.dismissTimer_) {
<a name="line234"></a>    goog.Timer.clear(this.dismissTimer_);
<a name="line235"></a>  }
<a name="line236"></a>  if (this.showTimer_) {
<a name="line237"></a>    goog.Timer.clear(this.showTimer_);
<a name="line238"></a>  }
<a name="line239"></a>};
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Sets the menu item to be visible or invisible.
<a name="line244"></a> * @param {boolean} visible Whether to show or hide the component.
<a name="line245"></a> * @param {boolean=} opt_force If true, doesn&#39;t check whether the component
<a name="line246"></a> *     already has the requested visibility, and doesn&#39;t dispatch any events.
<a name="line247"></a> * @return {boolean} Whether the visibility was changed.
<a name="line248"></a> * @override
<a name="line249"></a> */
<a name="line250"></a>goog.ui.SubMenu.prototype.setVisible = function(visible, opt_force) {
<a name="line251"></a>  var visibilityChanged = goog.ui.SubMenu.superClass_.setVisible.call(this,
<a name="line252"></a>      visible, opt_force);
<a name="line253"></a>  // For menus that allow menu items to be hidden (i.e. ComboBox) ensure that
<a name="line254"></a>  // the submenu is hidden.
<a name="line255"></a>  if (visibilityChanged &amp;&amp; !this.isVisible()) {
<a name="line256"></a>    this.dismissSubMenu();
<a name="line257"></a>  }
<a name="line258"></a>  return visibilityChanged;
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Dismiss all the sub menus of sibling menu items.
<a name="line264"></a> * @private
<a name="line265"></a> */
<a name="line266"></a>goog.ui.SubMenu.prototype.dismissSiblings_ = function() {
<a name="line267"></a>  this.getParent().forEachChild(function(child) {
<a name="line268"></a>    if (child != this &amp;&amp; typeof child.dismissSubMenu == &#39;function&#39;) {
<a name="line269"></a>      child.dismissSubMenu();
<a name="line270"></a>      child.clearTimers();
<a name="line271"></a>    }
<a name="line272"></a>  }, this);
<a name="line273"></a>};
<a name="line274"></a>
<a name="line275"></a>
<a name="line276"></a>/**
<a name="line277"></a> * Handles a key event that is passed to the menu item from its parent because
<a name="line278"></a> * it is highlighted.  If the right key is pressed the sub menu takes control
<a name="line279"></a> * and delegates further key events to its menu until it is dismissed OR the
<a name="line280"></a> * left key is pressed.
<a name="line281"></a> * @param {goog.events.KeyEvent} e A key event.
<a name="line282"></a> * @return {boolean} Whether the event was handled.
<a name="line283"></a> * @override
<a name="line284"></a> */
<a name="line285"></a>goog.ui.SubMenu.prototype.handleKeyEvent = function(e) {
<a name="line286"></a>  var keyCode = e.keyCode;
<a name="line287"></a>  var openKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.LEFT :
<a name="line288"></a>      goog.events.KeyCodes.RIGHT;
<a name="line289"></a>  var closeKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.RIGHT :
<a name="line290"></a>      goog.events.KeyCodes.LEFT;
<a name="line291"></a>
<a name="line292"></a>  if (!this.hasKeyboardControl_) {
<a name="line293"></a>    // Menu item doesn&#39;t have keyboard control and the right key was pressed.
<a name="line294"></a>    // So open take keyboard control and open the sub menu.
<a name="line295"></a>    if (this.isEnabled() &amp;&amp;
<a name="line296"></a>        (keyCode == openKeyCode || keyCode == this.getMnemonic())) {
<a name="line297"></a>      this.showSubMenu();
<a name="line298"></a>      this.getMenu().highlightFirst();
<a name="line299"></a>      this.clearTimers();
<a name="line300"></a>
<a name="line301"></a>    // The menu item doesn&#39;t currently care about the key events so let the
<a name="line302"></a>    // parent menu handle them accordingly .
<a name="line303"></a>    } else {
<a name="line304"></a>      return false;
<a name="line305"></a>    }
<a name="line306"></a>
<a name="line307"></a>  // Menu item has control, so let its menu try to handle the keys (this may
<a name="line308"></a>  // in turn be handled by sub-sub menus).
<a name="line309"></a>  } else if (this.getMenu().handleKeyEvent(e)) {
<a name="line310"></a>    // Nothing to do
<a name="line311"></a>
<a name="line312"></a>  // The menu has control and the key hasn&#39;t yet been handled, on left arrow
<a name="line313"></a>  // we turn off key control.
<a name="line314"></a>  } else if (keyCode == closeKeyCode) {
<a name="line315"></a>    this.dismissSubMenu();
<a name="line316"></a>
<a name="line317"></a>  } else {
<a name="line318"></a>    // Submenu didn&#39;t handle the key so let the parent decide what to do.
<a name="line319"></a>    return false;
<a name="line320"></a>  }
<a name="line321"></a>
<a name="line322"></a>  e.preventDefault();
<a name="line323"></a>  return true;
<a name="line324"></a>};
<a name="line325"></a>
<a name="line326"></a>
<a name="line327"></a>/**
<a name="line328"></a> * Listens to the sub menus items and ensures that this menu item is selected
<a name="line329"></a> * while dismissing the others.  This handles the case when the user mouses
<a name="line330"></a> * over other items on their way to the sub menu.
<a name="line331"></a> * @param {goog.events.Event} e Highlight event to handle.
<a name="line332"></a> * @private
<a name="line333"></a> */
<a name="line334"></a>goog.ui.SubMenu.prototype.onChildHighlight_ = function(e) {
<a name="line335"></a>  if (this.subMenu_.getParent() == this) {
<a name="line336"></a>    this.clearTimers();
<a name="line337"></a>    this.getParentEventTarget().setHighlighted(this);
<a name="line338"></a>    this.dismissSiblings_();
<a name="line339"></a>  }
<a name="line340"></a>};
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/**
<a name="line344"></a> * Listens to the parent menu&#39;s hide event and ensures that all submenus are
<a name="line345"></a> * hidden at the same time.
<a name="line346"></a> * @param {goog.events.Event} e The event.
<a name="line347"></a> * @private
<a name="line348"></a> */
<a name="line349"></a>goog.ui.SubMenu.prototype.onParentHidden_ = function(e) {
<a name="line350"></a>  // Ignore propagated events
<a name="line351"></a>  if (e.target == this.getParentEventTarget()) {
<a name="line352"></a>    // TODO(user): Using an event for this is expensive.  Consider having a
<a name="line353"></a>    // generalized interface that the parent menu calls on its children when
<a name="line354"></a>    // it is hidden.
<a name="line355"></a>    this.dismissSubMenu();
<a name="line356"></a>    this.clearTimers();
<a name="line357"></a>  }
<a name="line358"></a>};
<a name="line359"></a>
<a name="line360"></a>
<a name="line361"></a>/**
<a name="line362"></a> * @override
<a name="line363"></a> * Sets a timer to show the submenu and then dispatches an ENTER event to the
<a name="line364"></a> * parent menu.
<a name="line365"></a> * @param {goog.events.BrowserEvent} e Mouse event to handle.
<a name="line366"></a> * @protected
<a name="line367"></a> */
<a name="line368"></a>goog.ui.SubMenu.prototype.handleMouseOver = function(e) {
<a name="line369"></a>  if (this.isEnabled()) {
<a name="line370"></a>    this.clearTimers();
<a name="line371"></a>    this.showTimer_ = goog.Timer.callOnce(
<a name="line372"></a>        this.showSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
<a name="line373"></a>  }
<a name="line374"></a>  goog.ui.SubMenu.superClass_.handleMouseOver.call(this, e);
<a name="line375"></a>};
<a name="line376"></a>
<a name="line377"></a>
<a name="line378"></a>/**
<a name="line379"></a> * Overrides the default mouseup event handler, so that the ACTION isn&#39;t
<a name="line380"></a> * dispatched for the submenu itself, instead the submenu is shown instantly.
<a name="line381"></a> * @param {goog.events.Event} e The browser event.
<a name="line382"></a> * @return {boolean} True if the action was allowed to proceed, false otherwise.
<a name="line383"></a> * @override
<a name="line384"></a> */
<a name="line385"></a>goog.ui.SubMenu.prototype.performActionInternal = function(e) {
<a name="line386"></a>  this.clearTimers();
<a name="line387"></a>  var shouldHandleClick = this.isSupportedState(
<a name="line388"></a>      goog.ui.Component.State.SELECTED);
<a name="line389"></a>  if (shouldHandleClick) {
<a name="line390"></a>    return goog.ui.SubMenu.superClass_.performActionInternal.call(this, e);
<a name="line391"></a>  } else {
<a name="line392"></a>    this.showSubMenu();
<a name="line393"></a>    return true;
<a name="line394"></a>  }
<a name="line395"></a>};
<a name="line396"></a>
<a name="line397"></a>
<a name="line398"></a>/**
<a name="line399"></a> * Sets the visiblility of the sub menu.
<a name="line400"></a> * @param {boolean} visible Whether to show menu.
<a name="line401"></a> * @private
<a name="line402"></a> */
<a name="line403"></a>goog.ui.SubMenu.prototype.setSubMenuVisible_ = function(visible) {
<a name="line404"></a>  // Dispatch OPEN event before calling getMenu(), so we can create the menu
<a name="line405"></a>  // lazily on first access.
<a name="line406"></a>  this.dispatchEvent(goog.ui.Component.getStateTransitionEvent(
<a name="line407"></a>      goog.ui.Component.State.OPENED, visible));
<a name="line408"></a>  var subMenu = this.getMenu();
<a name="line409"></a>  if (visible != subMenu.isVisible()) {
<a name="line410"></a>    if (visible) {
<a name="line411"></a>      // Lazy-render menu when first shown, if needed.
<a name="line412"></a>      if (!subMenu.isInDocument()) {
<a name="line413"></a>        subMenu.render();
<a name="line414"></a>      }
<a name="line415"></a>      subMenu.setHighlightedIndex(-1);
<a name="line416"></a>    }
<a name="line417"></a>    this.hasKeyboardControl_ = visible;
<a name="line418"></a>    goog.dom.classes.enable(this.getElement(),
<a name="line419"></a>        goog.getCssName(&#39;goog-submenu-open&#39;), visible);
<a name="line420"></a>    subMenu.setVisible(visible);
<a name="line421"></a>    // We must position after the menu is visible, otherwise positioning logic
<a name="line422"></a>    // breaks in RTL.
<a name="line423"></a>    if (visible) {
<a name="line424"></a>      this.positionSubMenu();
<a name="line425"></a>    }
<a name="line426"></a>  }
<a name="line427"></a>};
<a name="line428"></a>
<a name="line429"></a>
<a name="line430"></a>/**
<a name="line431"></a> * Attaches or detaches menu event listeners to/from the given menu.  Called
<a name="line432"></a> * each time a menu is attached to or detached from the submenu.
<a name="line433"></a> * @param {goog.ui.Menu} menu Menu on which to listen for events.
<a name="line434"></a> * @param {boolean} attach Whether to attach or detach event listeners.
<a name="line435"></a> * @private
<a name="line436"></a> */
<a name="line437"></a>goog.ui.SubMenu.prototype.setMenuListenersEnabled_ = function(menu, attach) {
<a name="line438"></a>  var handler = this.getHandler();
<a name="line439"></a>  var method = attach ? handler.listen : handler.unlisten;
<a name="line440"></a>  method.call(handler, menu, goog.ui.Component.EventType.HIGHLIGHT,
<a name="line441"></a>      this.onChildHighlight_);
<a name="line442"></a>};
<a name="line443"></a>
<a name="line444"></a>
<a name="line445"></a>/**
<a name="line446"></a> * Sets whether the submenu is aligned at the end of the parent menu.
<a name="line447"></a> * @param {boolean} alignToEnd True to align to end, false to align to start.
<a name="line448"></a> */
<a name="line449"></a>goog.ui.SubMenu.prototype.setAlignToEnd = function(alignToEnd) {
<a name="line450"></a>  if (alignToEnd != this.alignToEnd_) {
<a name="line451"></a>    this.alignToEnd_ = alignToEnd;
<a name="line452"></a>    if (this.isInDocument()) {
<a name="line453"></a>      // Completely re-render the widget.
<a name="line454"></a>      var oldElement = this.getElement();
<a name="line455"></a>      this.exitDocument();
<a name="line456"></a>
<a name="line457"></a>      if (oldElement.nextSibling) {
<a name="line458"></a>        this.renderBefore(/** @type {!Element} */ (oldElement.nextSibling));
<a name="line459"></a>      } else {
<a name="line460"></a>        this.render(/** @type {Element} */ (oldElement.parentNode));
<a name="line461"></a>      }
<a name="line462"></a>    }
<a name="line463"></a>  }
<a name="line464"></a>};
<a name="line465"></a>
<a name="line466"></a>
<a name="line467"></a>/**
<a name="line468"></a> * Determines whether the submenu is aligned at the end of the parent menu.
<a name="line469"></a> * @return {boolean} True if aligned to the end (the default), false if
<a name="line470"></a> *     aligned to the start.
<a name="line471"></a> */
<a name="line472"></a>goog.ui.SubMenu.prototype.isAlignedToEnd = function() {
<a name="line473"></a>  return this.alignToEnd_;
<a name="line474"></a>};
<a name="line475"></a>
<a name="line476"></a>
<a name="line477"></a>/**
<a name="line478"></a> * Positions the submenu. This method should be called if the sub menu is
<a name="line479"></a> * opened and the menu element&#39;s size changes (e.g., when adding/removing items
<a name="line480"></a> * to an opened sub menu).
<a name="line481"></a> */
<a name="line482"></a>goog.ui.SubMenu.prototype.positionSubMenu = function() {
<a name="line483"></a>  var position = new goog.positioning.AnchoredViewportPosition(
<a name="line484"></a>      this.getElement(), this.isAlignedToEnd() ?
<a name="line485"></a>      goog.positioning.Corner.TOP_END : goog.positioning.Corner.TOP_START,
<a name="line486"></a>      this.isPositionAdjustable_);
<a name="line487"></a>
<a name="line488"></a>  // TODO(user): Clean up popup code and have this be a one line call
<a name="line489"></a>  var subMenu = this.getMenu();
<a name="line490"></a>  var el = subMenu.getElement();
<a name="line491"></a>  if (!subMenu.isVisible()) {
<a name="line492"></a>    el.style.visibility = &#39;hidden&#39;;
<a name="line493"></a>    goog.style.showElement(el, true);
<a name="line494"></a>  }
<a name="line495"></a>
<a name="line496"></a>  position.reposition(
<a name="line497"></a>      el, this.isAlignedToEnd() ?
<a name="line498"></a>      goog.positioning.Corner.TOP_START : goog.positioning.Corner.TOP_END);
<a name="line499"></a>
<a name="line500"></a>  if (!subMenu.isVisible()) {
<a name="line501"></a>    goog.style.showElement(el, false);
<a name="line502"></a>    el.style.visibility = &#39;visible&#39;;
<a name="line503"></a>  }
<a name="line504"></a>};
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>// Methods delegated to sub-menu but accessible here for convinience
<a name="line508"></a>
<a name="line509"></a>
<a name="line510"></a>/**
<a name="line511"></a> * Adds a new menu item at the end of the menu.
<a name="line512"></a> * @param {goog.ui.MenuHeader|goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu
<a name="line513"></a> *     item to add to the menu.
<a name="line514"></a> */
<a name="line515"></a>goog.ui.SubMenu.prototype.addItem = function(item) {
<a name="line516"></a>  this.getMenu().addChild(item, true);
<a name="line517"></a>};
<a name="line518"></a>
<a name="line519"></a>
<a name="line520"></a>/**
<a name="line521"></a> * Adds a new menu item at a specific index in the menu.
<a name="line522"></a> * @param {goog.ui.MenuHeader|goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu
<a name="line523"></a> *     item to add to the menu.
<a name="line524"></a> * @param {number} n Index at which to insert the menu item.
<a name="line525"></a> */
<a name="line526"></a>goog.ui.SubMenu.prototype.addItemAt = function(item, n) {
<a name="line527"></a>  this.getMenu().addChildAt(item, n, true);
<a name="line528"></a>};
<a name="line529"></a>
<a name="line530"></a>
<a name="line531"></a>/**
<a name="line532"></a> * Removes an item from the menu and disposes it.
<a name="line533"></a> * @param {goog.ui.MenuItem} item The menu item to remove.
<a name="line534"></a> */
<a name="line535"></a>goog.ui.SubMenu.prototype.removeItem = function(item) {
<a name="line536"></a>  var child = this.getMenu().removeChild(item, true);
<a name="line537"></a>  if (child) {
<a name="line538"></a>    child.dispose();
<a name="line539"></a>  }
<a name="line540"></a>};
<a name="line541"></a>
<a name="line542"></a>
<a name="line543"></a>/**
<a name="line544"></a> * Removes a menu item at a given index in the menu and disposes it.
<a name="line545"></a> * @param {number} n Index of item.
<a name="line546"></a> */
<a name="line547"></a>goog.ui.SubMenu.prototype.removeItemAt = function(n) {
<a name="line548"></a>  var child = this.getMenu().removeChildAt(n, true);
<a name="line549"></a>  if (child) {
<a name="line550"></a>    child.dispose();
<a name="line551"></a>  }
<a name="line552"></a>};
<a name="line553"></a>
<a name="line554"></a>
<a name="line555"></a>/**
<a name="line556"></a> * Returns a reference to the menu item at a given index.
<a name="line557"></a> * @param {number} n Index of menu item.
<a name="line558"></a> * @return {goog.ui.Component} Reference to the menu item.
<a name="line559"></a> */
<a name="line560"></a>goog.ui.SubMenu.prototype.getItemAt = function(n) {
<a name="line561"></a>  return this.getMenu().getChildAt(n);
<a name="line562"></a>};
<a name="line563"></a>
<a name="line564"></a>
<a name="line565"></a>/**
<a name="line566"></a> * Returns the number of items in the sub menu (including separators).
<a name="line567"></a> * @return {number} The number of items in the menu.
<a name="line568"></a> */
<a name="line569"></a>goog.ui.SubMenu.prototype.getItemCount = function() {
<a name="line570"></a>  return this.getMenu().getChildCount();
<a name="line571"></a>};
<a name="line572"></a>
<a name="line573"></a>
<a name="line574"></a>/**
<a name="line575"></a> * Returns the menu items contained in the sub menu.
<a name="line576"></a> * @return {Array.&lt;goog.ui.MenuItem&gt;} An array of menu items.
<a name="line577"></a> * @deprecated Use getItemAt/getItemCount instead.
<a name="line578"></a> */
<a name="line579"></a>goog.ui.SubMenu.prototype.getItems = function() {
<a name="line580"></a>  return this.getMenu().getItems();
<a name="line581"></a>};
<a name="line582"></a>
<a name="line583"></a>
<a name="line584"></a>/**
<a name="line585"></a> * Gets a reference to the submenu&#39;s actual menu.
<a name="line586"></a> * @return {goog.ui.Menu} Reference to the object representing the sub menu.
<a name="line587"></a> */
<a name="line588"></a>goog.ui.SubMenu.prototype.getMenu = function() {
<a name="line589"></a>  if (!this.subMenu_) {
<a name="line590"></a>    this.setMenu(
<a name="line591"></a>        new goog.ui.Menu(this.getDomHelper()), /* opt_internal */ true);
<a name="line592"></a>  } else if (this.externalSubMenu_ &amp;&amp; this.subMenu_.getParent() != this) {
<a name="line593"></a>    // Since it is possible for the same popup menu to be attached to multiple
<a name="line594"></a>    // submenus, we need to ensure that it has the correct parent event target.
<a name="line595"></a>    this.subMenu_.setParent(this);
<a name="line596"></a>  }
<a name="line597"></a>  // Always create the menu DOM, for backward compatibility.
<a name="line598"></a>  if (!this.subMenu_.getElement()) {
<a name="line599"></a>    this.subMenu_.createDom();
<a name="line600"></a>  }
<a name="line601"></a>  return this.subMenu_;
<a name="line602"></a>};
<a name="line603"></a>
<a name="line604"></a>
<a name="line605"></a>/**
<a name="line606"></a> * Sets the submenu to a specific menu.
<a name="line607"></a> * @param {goog.ui.Menu} menu The menu to show when this item is selected.
<a name="line608"></a> * @param {boolean=} opt_internal Whether this menu is an &quot;internal&quot; menu, and
<a name="line609"></a> *     should be disposed of when this object is disposed of.
<a name="line610"></a> */
<a name="line611"></a>goog.ui.SubMenu.prototype.setMenu = function(menu, opt_internal) {
<a name="line612"></a>  var oldMenu = this.subMenu_;
<a name="line613"></a>  if (menu != oldMenu) {
<a name="line614"></a>    if (oldMenu) {
<a name="line615"></a>      this.dismissSubMenu();
<a name="line616"></a>      if (this.isInDocument()) {
<a name="line617"></a>        this.setMenuListenersEnabled_(oldMenu, false);
<a name="line618"></a>      }
<a name="line619"></a>    }
<a name="line620"></a>
<a name="line621"></a>    this.subMenu_ = menu;
<a name="line622"></a>    this.externalSubMenu_ = !opt_internal;
<a name="line623"></a>
<a name="line624"></a>    if (menu) {
<a name="line625"></a>      menu.setParent(this);
<a name="line626"></a>      // There&#39;s no need to dispatch a HIDE event during submenu construction.
<a name="line627"></a>      menu.setVisible(false, /* opt_force */ true);
<a name="line628"></a>      menu.setAllowAutoFocus(false);
<a name="line629"></a>      menu.setFocusable(false);
<a name="line630"></a>      if (this.isInDocument()) {
<a name="line631"></a>        this.setMenuListenersEnabled_(menu, true);
<a name="line632"></a>      }
<a name="line633"></a>    }
<a name="line634"></a>  }
<a name="line635"></a>};
<a name="line636"></a>
<a name="line637"></a>
<a name="line638"></a>/**
<a name="line639"></a> * Returns true if the provided element is to be considered inside the menu for
<a name="line640"></a> * purposes such as dismissing the menu on an event.  This is so submenus can
<a name="line641"></a> * make use of elements outside their own DOM.
<a name="line642"></a> * @param {Element} element The element to test for.
<a name="line643"></a> * @return {boolean} Whether or not the provided element is contained.
<a name="line644"></a> */
<a name="line645"></a>goog.ui.SubMenu.prototype.containsElement = function(element) {
<a name="line646"></a>  return this.getMenu().containsElement(element);
<a name="line647"></a>};
<a name="line648"></a>
<a name="line649"></a>
<a name="line650"></a>/**
<a name="line651"></a> * @param {boolean} isAdjustable Whether this submenu is adjustable.
<a name="line652"></a> */
<a name="line653"></a>goog.ui.SubMenu.prototype.setPositionAdjustable = function(isAdjustable) {
<a name="line654"></a>  this.isPositionAdjustable_ = !!isAdjustable;
<a name="line655"></a>};
<a name="line656"></a>
<a name="line657"></a>
<a name="line658"></a>/**
<a name="line659"></a> * @return {boolean} Whether this submenu is adjustable.
<a name="line660"></a> */
<a name="line661"></a>goog.ui.SubMenu.prototype.isPositionAdjustable = function() {
<a name="line662"></a>  return this.isPositionAdjustable_;
<a name="line663"></a>};
<a name="line664"></a>
<a name="line665"></a>
<a name="line666"></a>// Register a decorator factory function for goog.ui.SubMenus.
<a name="line667"></a>goog.ui.registry.setDecoratorByClassName(goog.getCssName(&#39;goog-submenu&#39;),
<a name="line668"></a>    function() {
<a name="line669"></a>      return new goog.ui.SubMenu(null);
<a name="line670"></a>    });
</pre>


</body>
</html>
